package com.jeejio.common.rcv.decoration

import android.graphics.Rect
import android.view.View
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.ItemDecoration

/**
 * Author: JFangZ
 * Email: janezhang1901@163.com
 * Date: 2021/4/16 18:13
 * Description:The decoration to add safe distance.
 */
class SafeDistanceDecoration private constructor(builder: Builder) : ItemDecoration() {
    private var left = 0
    private var top = 0
    private var right = 0
    private var bottom = 0

    init {
        left = builder.left
        top = builder.top
        right = builder.right
        bottom = builder.bottom
    }

    override fun getItemOffsets(outRect: Rect, view: View, parent: RecyclerView, state: RecyclerView.State) {
        val position = parent.getChildAdapterPosition(view)
        // 获取当前itemView的位置
        val adapter = parent.adapter ?: return
        val layoutManager = parent.layoutManager
        if (layoutManager is LinearLayoutManager) {
            // LinearLayoutManager
            if (layoutManager.orientation == RecyclerView.VERTICAL) {
                // 纵向分隔线，第一个和最后一个
                outRect.left = left
                outRect.right = right
                if (position == 0) {
                    outRect.top = top
                }
                if (position == adapter.itemCount - 1) {
                    outRect.bottom = bottom
                }
            } else if (layoutManager.orientation == RecyclerView.HORIZONTAL) {
                // 横向分隔线，第一个和最后一个
                outRect.top = top
                outRect.bottom = bottom
                if (position == 0) {
                    outRect.left = left
                }
                if (position == adapter.itemCount - 1) {
                    outRect.right = right
                }
            }
        } else if (layoutManager is GridLayoutManager) {
            // GridLayoutManager
            val spanCount = layoutManager.spanCount
            // 第一行
            if (position < spanCount) {
                outRect.top = top
            }
            // 最后一行
            if (position > adapter.itemCount - spanCount) {
                outRect.bottom = bottom
            }
            // 每一行第一个 item，左边
            if (position % spanCount == 0) {
                outRect.left = left
            }
            // 每一行最后一个 item，右边
            if (position % spanCount == spanCount - 1) {
                outRect.right = right
            }
        }
    }

    class Builder {
        internal var left = 0
        internal var top = 0
        internal var right = 0
        internal var bottom = 0
        fun setLeft(left: Int): Builder {
            this.left = left
            return this
        }

        fun setTop(top: Int): Builder {
            this.top = top
            return this
        }

        fun setRight(right: Int): Builder {
            this.right = right
            return this
        }

        fun setBottom(bottom: Int): Builder {
            this.bottom = bottom
            return this
        }

        fun build() = SafeDistanceDecoration(this)
    }
}